const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    CarInfo: 1,   //其值是预渲染模板
    questionList: [],
  },
  onLoad(options){
    this.setData({
      search_val: options.search_val
    });
    this.getMotorcycleType();
  },
  /*分享*/
  onShareAppMessage(){

  },

  /*初始数据*/ 
  InitData(){
    let _this;
    if(!_this){
      _this = this;      
    }
    return {
      loading(){
        _this.setData({
          showInitLoading: false,
          RequestStatus: true
        });
      }
    }
  },

  /*筛选*/ 
  filterVehicleList(params){
    let _this = this, td = this.data;
    params.thirdCookie = wx.getStorageSync("thirdCookie");
    if (td.search_val) {
      _this.InitData().loading();
      SERVICE.POST(SERVICE.URL.filterVehicleList, params, (status, res) => {
        console.log("筛选:", res);
        if (status) {
          // if (res.data.res_msg.result.questionList) {
          //   res.data.res_msg.result.questionList.selected = false;            
          //     td.questionList.push(res.data.res_msg.result.questionList);
          //     for (let i = 0; i < td.questionList.length; i++) {
          //       if (td.questionList.question == res.data.res_msg.result.questionList.question) {
          //         td.questionList.splice(i, 1);
          //       }
          //     }
          // }
          let questionList = res.data.res_msg.result.questionList;
          _this.setData({
            showInitLoading: true,
            CarInfo: res.data.res_msg.result.vehicleList,
            questionList: questionList,
            vehicleNum: res.data.res_msg.vehicle_num
          });
        } else {
          _this.setData({
            showInitLoading: true,
            CarInfo: null,
          });
        }
      }, function fail() {
        _this.setData({
          RequestStatus: false
        });
      });
    }
  },

  getMountings(e){
   let td = this.data, index = e.currentTarget.dataset.index,
    val = e.currentTarget.dataset.val,
    item = e.currentTarget.dataset.item,
    idx = e.currentTarget.dataset.idx;
    var list = [];
      for (let i = 0; i < item.length; i++){
      if (i <= idx){
        list.push(item[i]);
      }
    }

    this.setData({
      showItem: idx,
      showChildIndex: index
    });
    this.filterVehicleList({
      questionList: JSON.stringify(list),
      answerValue: val,
    });
  },

  /*获取车辆信息*/ 
  getMotorcycleType(searchval){
    let _this = this, td = this.data;
    if (td.search_val){
      _this.InitData().loading();
     SERVICE.POST(SERVICE.URL.getMotorcycleType, {
       thirdCookie: wx.getStorageSync("thirdCookie"),
      vinCode: td.search_val
     }, (status, res) => {
       console.log("获取车辆信息:",res);
       if (status) {
         let questionList = res.data.res_msg.result.questionList;
         _this.setData({
           showInitLoading: true,
           vehicleNum: res.data.res_msg.vehicle_num,
           CarInfo: res.data.res_msg.result.vehicleList,
           questionList: questionList
         });
       }else{
         _this.setData({
           showInitLoading: true,
           CarInfo:null,
         });
       }
     },function fail(){
       _this.setData({
         RequestStatus: false                    
       });
     });
    }
  },

  RequestCellBack(){
    this.getMotorcycleType();
  },
  info(e){
    let td = this.data, item = e.currentTarget.dataset.item;
    // wx.navigateTo({
    //   url: "../fittings-inquiry/fittings-inquiry?vehicleId=" + e.currentTarget.dataset.id +
    //     '&CarInfo=' + JSON.stringify(td.CarInfo)
    // });
    console.log(item);
    wx.navigateTo({
      url: "../fittings-inquiry/fittings-inquiry?vehicleId=" + e.currentTarget.dataset.id +
        '&CarInfo=' + JSON.stringify(item)
    });
    
    // 缓存搜索VIN
    wx.setStorageSync("VIN", td.search_val);
  }
});